/**
 * 
 */
package com.alianzamedica.controllers.drug;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Drug;
import com.alianzamedica.tools.Enviroment;

/**
 * @author RMarquez_Inf
 *
 */
public class DrugDeleteAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		String sId = request.getParameter("id");
		Integer id = null;
		if (sId != null) {
			try {
				id = Integer.valueOf(sId);
			} catch (Exception e) {
			}
		}
		if (id != null) {
			Drug patient = new Drug();
			patient.setId(id);
			search.deleteObject(patient);
		}
		String mensaje = "Se ha borrado el medicamento exitosamente";
		String linkRetorno = "/admin/drug/list.do";
		request.setAttribute("mensaje", mensaje);
		request.setAttribute("linkRetorno", linkRetorno);

		return mapping.findForward("success");
	}

}
